/**
 * @file luogu/3879/main
 * @brief
 * @see
 * @author Ruiming Guo (guoruiming@stu.scu.edu.cn)
 * @copyright 2022
 * @date 2022/6/21 20:45:50
 **/

#include <bitset>
#include <iostream>
using namespace std;
int n, m;
char s[1010];
int l;
int tot = 0;
int trie[300007][26];
vector<short> v[300007];
void insert(char *s, int x) {
  int rt = 0;
  for (int i = 0; s[i]; ++i) {
    int v = s[i] - 'a';
    if (!trie[rt][v]) trie[rt][v] = ++tot;
    rt = trie[rt][v];
  }
  b[rt][x] = 1;
}
void query(char *s) {
  int rt = 0;
  for (int i = 0; s[i]; ++i) {
    int v = s[i] - 'a';
    if (!trie[rt][v]) {
      cout << ' ' << endl;
      return;
    }
    rt = trie[rt][v];
  }
  for (int i = 1; i <= n; ++i) {
    if (b[rt][i] == 1) cout << i << ' ';
  }
  cout << endl;
}
int main() {
  cin >> n;
  for (int i = 1; i <= n; ++i) {
    cin >> l;
    for (int j = 1; j <= l; ++j) {
      cin >> s;
      insert(s, i);
    }
  }
  cin >> m;
  for (int i = 1; i <= m; ++i) {
    cin >> s;
    query(s);
  }
  return 0;
}
